package com.fieldchain.ex.fa100.access;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.CoinSufBean;
import com.fieldchain.ex.fa100.bean.event.MainEvent;
import com.fieldchain.ex.fa100.custom.view.MyRadioButton;
import com.fieldchain.ex.fa100.ui.access.C2CFragment;
import com.fieldchain.ex.fa100.ui.access.MarketFragment;
import com.fieldchain.ex.fa100.ui.access.MoneyFragment;
import com.fieldchain.ex.fa100.ui.access.PushFragment;
import com.fieldchain.ex.fa100.ui.access.TradFragment;
import com.fieldchain.ex.fa100.ui.c2c.activity.C2CHistoryActivity;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends MyBaseActivity {
    private FrameLayout homeFragment;
    private RadioGroup rgHome;
    private MyRadioButton rdHome;
    private MyRadioButton rdTrade;
    private MyRadioButton rdC2c;
    private MyRadioButton rdPush;
    private MyRadioButton rdMoney;
    private Fragment[] fragments = new Fragment[5];
    private int index = 0, oldindex = 0;
    private long oldTime, newTime;
    private String market = "CMS_USDT";

    private List<CoinSufBean> mList = new ArrayList<>();


    @Override
    public void findView() {
        homeFragment = (FrameLayout) findViewById(R.id.home_fragment);
        rgHome = (RadioGroup) findViewById(R.id.rg_home);
        rdHome = (MyRadioButton) findViewById(R.id.rd_home);
        rdTrade = (MyRadioButton) findViewById(R.id.rd_trade);
        rdC2c = (MyRadioButton) findViewById(R.id.rd_c2c);
        rdPush = (MyRadioButton) findViewById(R.id.rd_push);
        rdMoney = (MyRadioButton) findViewById(R.id.rd_money);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        fragments = null;
    }


    public void setValue() {
        needProgress = false;
        getCoin();
        setTags(rdHome, 0);
        setTags(rdTrade, 1);
        setTags(rdPush, 2);
        setTags(rdC2c, 3);
        setTags(rdMoney, 4);

        fragments[0] = new MarketFragment();
        fragments[1] = new TradFragment();
        fragments[2] = new PushFragment();
        fragments[3] = new C2CFragment();
        fragments[4] = new MoneyFragment();

        FragmentTransaction transaction_home = getSupportFragmentManager().beginTransaction();
        transaction_home.add(R.id.home_fragment, fragments[0]);
        transaction_home.commit();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    public void setTags(RadioButton bt, final int tag) {
        bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    index = tag;
                    if (index != oldindex) {
                        switchFragment();
                    }
                }
            }
        });
    }

    //正确的做法
    private void switchFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.home_fragment, fragments[index]).commit();
        oldindex = index;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void mainEvent(MainEvent et) {
        int pagTag = et.getPage();
        if (pagTag == 1) {
            market = et.getMarket();
            ((TradFragment) fragments[pagTag]).refresh(et.getMarket(), et.getType());
        } else if (pagTag == 3) {
            if (et.getType() == 2) {
                startActivity(new Intent(MainActivity.this, C2CHistoryActivity.class));
            } else {
                ((C2CFragment) fragments[pagTag]).refresh(et.getMarket(), et.getType());
            }
        }
        index = pagTag;
        if (oldindex != index) {
            if (index == 3 && et.getType() == 2) {
                index = oldindex;
            } else {
                ((RadioButton) rgHome.getChildAt(index)).setChecked(true);
            }
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            newTime = System.currentTimeMillis();
            if ((newTime - oldTime) > 2000) {
                MyToast.show("再按一次退出程序");
                oldTime = newTime;
            } else {
                System.exit(0);
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100) {
            index = 1;
            ((RadioButton) rgHome.getChildAt(index)).setChecked(true);
            ((TradFragment) fragments[index]).refresh(data.getStringExtra("market"), 0);
        }
    }

    @Override
    public void loginShow() {

    }

    //屏幕方向发生改变的回调方法
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.e("newConfig.orientation", newConfig.orientation + "");
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onConfigurationChanged(newConfig);

    }

    public void getCoin() {
        HttpRequest.getInstance().getRequest(Constant.trad_coin, new ListRequest<CoinSufBean>() {
            @Override
            public void onSucess(List<CoinSufBean> list, int code, String msg) {
                if (code == 200) {
                    Log.e("--->", "出错了");
                    if (list != null && list.size() > 0) {
                        market = list.get(0).getListMarket().get(0);
                        mList.clear();
                        mList.addAll(list);
                    }
                }

            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public String getMarket() {
        return market;
    }

    public List<CoinSufBean> getCoinSuf() {
        return mList;
    }
}
